import 'dart:math';

class Point {
  int x, y;
  late num num1, num2;

  Point(this.x, this.y) {
    num1 = x + 0.1;
    num2 = x + 0.2;
  }

  // 命名构造函数

  Point.origin() : this(0, 0);

  Point.fromJson(Map<String, dynamic> json) : this(json['x'], json['y']);

  distanceTo() {
    return sqrt(x * x + y * y);
  }
}

class ImmutablePoint {
  static final origin = const ImmutablePoint(0, 1);

  final int x;

  final int y;

  const ImmutablePoint(this.x, this.y);

  /*铺货*/
  @override
  noSuchMethod(Invocation invocation) {
    return super.noSuchMethod(invocation);
  }
}

class Logger {
  final String name;
  final Map<String, dynamic> _cache = {};

  // 私有构造函数
  Logger._internal(this.name);

  // 静态映射存储实例
  static final Map<String, Logger> _loggers = {};

  // factory 构造函数实现缓存机制
  factory Logger(String name) {
    if (_loggers.containsKey(name)) {
      return _loggers[name]!;
    } else {
      final logger = Logger._internal(name);
      _loggers[name] = logger;
      return logger;
    }
  }

  // 缓存对象方法
  void cacheObject(String key, dynamic value) {
    _cache[key] = value;
  }

  // 获取缓存对象方法
  T? getCachedObject<T>(String key) {
    return _cache[key] as T?;
  }

  // 检查缓存是否存在
  bool hasCache(String key) {
    return _cache.containsKey(key);
  }

  // 清除特定缓存
  void clearCache(String key) {
    _cache.remove(key);
  }

  // 清除所有缓存
  void clearAllCache() {
    _cache.clear();
  }

  // 日志输出方法
  void info(String message) {
    print('[$name] INFO: $message');
  }

  void error(String message) {
    print('[$name] ERROR: $message');
  }

  void debug(String message) {
    print('[$name] DEBUG: $message');
  }

  // 获取当前缓存大小
  int get cacheSize => _cache.length;
}

class Vector {
  final double x;
  final double y;
  final double z;

  Vector(this.x, this.y, this.z);

  // 重写加法操作符
  Vector operator +(Vector other) {
    return Vector(x + other.x, y + other.y, z + other.z);
  }

  // 重写减法操作符
  Vector operator -(Vector other) {
    return Vector(x - other.x, y - other.y, z - other.z);
  }

  // 重写乘法操作符（数乘）
  Vector operator *(double scalar) {
    return Vector(x * scalar, y * scalar, z * scalar);
  }

  // 重写除法操作符
  Vector operator /(double scalar) {
    return Vector(x / scalar, y / scalar, z / scalar);
  }

  // 重写相等操作符
  @override
  bool operator ==(Object other) {
    if (other is Vector) {
      return x == other.x && y == other.y && z == other.z;
    }
    return false;
  }

  // 重写取负操作符
  Vector operator -() {
    return Vector(-x, -y, -z);
  }

  // 重写 hashCode
  @override
  int get hashCode => Object.hash(x, y, z);

  // 计算向量的模长
  double get magnitude => sqrt(x * x + y * y + z * z);

  // 向量点积
  double dot(Vector other) {
    return x * other.x + y * other.y + z * other.z;
  }

  // 向量叉积
  Vector cross(Vector other) {
    return Vector(
      y * other.z - z * other.y,
      z * other.x - x * other.z,
      x * other.y - y * other.x,
    );
  }

  // 重写 toString 方法
  @override
  String toString() {
    return 'Vector($x, $y, $z)';
  }
}

class MyClass {
  void existingMethod() {
    print("存在的方法");
  }

  @override
  dynamic noSuchMethod(Invocation invocation) {
    // print('调用了不存在的方法: ${Symbol.getName(invocation.memberName)}');
    return null;
  }
}

void main() {
  Logger("error").debug("debug");
  Logger("error").debug("error");

  Logger("test").debug("error");

  var names = ["李三", "王五"];
  print(names.runtimeType);
  List<int> fixedList = List.filled(5, 0);
  // dynamic test = ImmutablePoint(2, 2);
  // test.test();
  // 动态调用不存在的方法
  var obj = MyClass();
  // obj.nonExistingMethod(); // 会触发 noSuchMethod


}
